﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using BuXiaChu.OrderDishes.BLL.Passport;
using BuXiaChu.OrderDishes.BLL.CookieManager;

namespace BuXiaChu.OrderDishes.BLL.WebRuntime
{
    public class PassportModule : IHttpModule
    {
        public static List<string> VerifyPageExtensionList = new List<string>();
        #region IHttpModule 成员

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            HttpApplication Application = context;
            Application.BeginRequest += new EventHandler(Application_BeginRequest);
            Application.AuthorizeRequest += new EventHandler(Application_AuthorizeRequest);
            Application.EndRequest += new EventHandler(Application_EndRequest);

            VerifyPageExtensionList.Clear();
            VerifyPageExtensionList.Add(".aspx");
            VerifyPageExtensionList.Add(".ashx");
            VerifyPageExtensionList.Add(".html");
            VerifyPageExtensionList.Add(".htm");
        }

        void Application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            if (VerifyPageExtensionList.Contains(Path.GetExtension(app.Request.Url.AbsolutePath).ToLower()))
            {
                app.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            }
        }

        void Application_BeginRequest(object sender, EventArgs e)
        {
            //var a = "";
        }

        void Application_AuthorizeRequest(object sender, EventArgs e)
        {
            #region
            HttpApplication app = sender as HttpApplication;
            if (VerifyPageExtensionList.Contains(Path.GetExtension(app.Request.Url.AbsolutePath).ToLower()))
            {
                if (app.User != null && app.User.Identity.IsAuthenticated)
                {
                    LoginSysUserInfo userInfo = new LoginSysUserInfo();
                    if (!PassportManager.Instance.Validate(userInfo.SmartID, userInfo.UserID))
                    {
                        AdminCookieManager.SignOut();
                        app.Response.Write("该帐号在别处已登录。<a href='/index.aspx'>重新登录</a>");
                        app.Response.End();
                    }
                }
                else
                {
                    if (Path.GetFileNameWithoutExtension(app.Request.Url.AbsolutePath).ToLower() != "login")
                    {
                        var returnUrl = HttpUtility.UrlEncode(app.Request.Url.ToString());
                        var loginUrl = string.Format(System.Web.Security.FormsAuthentication.LoginUrl, returnUrl);

                        app.Response.Redirect(loginUrl, true);
                    }
                }
            }
            #endregion
        }

        #endregion
    }
}
